<?php
	include('config.php');
	include('mpd.php');
	
	# initiliaze mpd
	global $MPD;
	$MPD = new MPD($timeout=$cfg_mpd_timeout);

	include("radios.php");
	include("musics.php");

	define("PIMUSIC_OK",  "OK");
	define("PIMUSIC_ERROR",  "ERROR");

	function readRequest()
	{
		$raw = file_get_contents('php://input');
		return  json_decode($raw);
	}

	function response($code, $msg)
	{
		header('Content-Type: application/json');
		$arr = array ('code'=>$code,'msg'=>$msg);
		$rsp = json_encode($arr);
		die( $rsp  ) ;
	}

	function response_raw($raw)
	{
		$rsp = $raw;
		die($rsp);
	}
	function empty_obj(&$object)
	{
		$obj = (array)$object;
		if (empty($obj))
			return true;
		return false;
	}

	// decode json request
	$req = readRequest();

	// connect to mpd
	$connected = $MPD->connect($srv=$cfg_mpd_host,$port=$cfg_mpd_port);
	if ( is_null($connected) ) {
		response(500, PIMUSIC_ERROR );
	}
	else {
		$MPD->initRadios($radio_db=$cfg_radios_db);
	}

	function getpause($state){
		return '<div onclick="javascript:pause_music()">'.$state.'</div>';
	}

	// dispatch
	if ( $req->cmd == "player.load.page")
	{
		if ( empty_obj( $req->args->page ) )
			response(403, PIMUSIC_ERROR );

		switch ($req->args->page) {
			case 1:
				response_raw( get_radios() );
				break;
			case 2:
				response_raw( get_musics() );
				break;
			default:
				response_raw( get_radios() );
		}
	} 
	elseif ( $req->cmd == "player.load.folder")
	{
		if ( empty_obj( $req->args->name ) )
			response(403, PIMUSIC_ERROR );
		if ( is_null( $html = get_musics($req->args->name) ) ) response(500, PIMUSIC_ERROR );
		response_raw($html );
	}
	elseif ( $req->cmd == "player.volume.down")
	{
		if ( is_null( $ret = $MPD->volumeDown($cfg_mpd_volume) ) ) response(500, PIMUSIC_ERROR );
		response(200, PIMUSIC_OK );
	} 
	elseif ( $req->cmd == "player.volume.up")
	{
		if ( is_null( $ret = $MPD->volumeUp($cfg_mpd_volume) ) ) response(500, PIMUSIC_ERROR );
		response(200, PIMUSIC_OK );
	}
	elseif ( $req->cmd == "player.pause")
	{
		if ( is_null( $ret = $MPD->pausePlay() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->getState() ) ) response(500, PIMUSIC_ERROR );
		response(200,  getpause($ret) );
	}
	elseif ( $req->cmd == "player.stop")
	{
		if ( is_null( $ret = $MPD->stopPlay() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->getState() ) ) response(500, PIMUSIC_ERROR );
		response(200, getpause($ret) );
	}
	elseif ( $req->cmd == "player.play.folder")
	{
		if ( empty_obj( $req->args->name ) )
			response(403, PIMUSIC_ERROR );
		if ( empty_obj( $req->args->random ) )
			response(403, PIMUSIC_ERROR );


		if ( is_null( $ret = $MPD->clearPlaylist() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->appendPlaylist( $req->args->name ) ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->startPlay($id="", $random= $req->args->random ) ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->getState() ) ) response(500, PIMUSIC_ERROR );
		response(200, getpause($ret) );
	}
	elseif ( $req->cmd == "player.play.file")
	{
		if ( empty_obj( $req->args->name ) )
			response(403, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->clearPlaylist() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->appendPlaylist( $req->args->name ) ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->startPlay("") ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->getState() ) ) response(500, PIMUSIC_ERROR );
		response(200, getpause($ret) );
	}
	elseif ( $req->cmd == "player.play.radio")
	{
		if ( empty_obj( $req->args->name ) )
			response(403, PIMUSIC_ERROR );

		if ( is_null( $ret = $MPD->clearPlaylist() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->appendPlaylist( $MPD->radios[$req->args->name] ) ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->startPlay("") ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->getState() ) ) response(500, PIMUSIC_ERROR );
		response(200, getpause($ret) );
	}
	elseif ( $req->cmd == "player.play.next")
	{
		if ( is_null( $ret = $MPD->pausePlay() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->nextPlay() ) ) response(500, PIMUSIC_ERROR );
		response(200, PIMUSIC_OK );
	} 
	elseif ( $req->cmd == "player.play.previous")
	{
		if ( is_null( $ret = $MPD->pausePlay() ) ) response(500, PIMUSIC_ERROR );
		if ( is_null( $ret = $MPD->previousPlay() ) ) response(500, PIMUSIC_ERROR );
		response(200, PIMUSIC_OK );
	}
	else // invalid command
	{
		response(403, PIMUSIC_ERROR);
	}
?>